package com.oocl.hw1.task1.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

public class RegularExpressionUtil {

	/**
	 * 枚举： 简单正则表达式
	 * @author WUJA13
	 *
	 */
	public static enum Regular{
		///字母
		ALPHABET("[a-zA-Z]"),
		///数字
		NUMBER("\\d"),
		///中文汉字
		CHINESE_CHARACTER("[\\u4e00-\\u9fa5]"),
		///中文标点符号
		CHINESE_PUNCTUATION("[\\u3002\\uff1b\\uff0c\\uff1a\\u201c\\u201d\\uff08\\uff09\\u3001\\uff1f\\u300a\\u300b\\uff01\\uff1f]"),
		///英文标点符号
		ENGLISH_PUNCTUATION("[,\\.!\\?\\[\\]\\(\\)\\{\\}<>:;\"\'`]"),
		///邮箱地址
		EMAIL("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$"),
		///生日
		BIRTHDAY("((19\\d{2})|(20((0\\d)|(1[0-7]))))-((0?[1-9])|(1?\\d))-(([0-2]?\\d)|(3[0-1]))"),
		//数字串
		NUMBERS("^\\d+$"),
		///姓名
		NAME("(\\w+|[\\u4e00-\\u9fa5]+)"),
		///性别
		SEX("([mM]ale|[Ff]emale)");
		
		private String reg;
		
		public String reg(){
			return this.reg;
		}
		
		private Regular(String reg){
			this.reg = reg;
		}
	}
	
	
	/**
	 * 统计匹配的片段数量
	 * @param regular
	 * @param target
	 * @return
	 */
	public static int statistics(Regular regular, String target){
		if(target==null || target.length()==0) return 0;
		
		int count = 0;
		Pattern pattern = Pattern.compile(regular.reg());
		Matcher  matcher = pattern.matcher(target);
		while(matcher.find()){
//			System.out.println( regular+" : "+ matcher.group());
			count++;
		}
		return count;
	}
			 
	
	/**
	 * 判断 某个字符串是否匹配指定的表达式
	 * @param regular 指定的正则表达式
	 * @param target  目标字符串
	 * @return 是否匹配
	 */
	public static boolean match(Regular regular , String target){
		return target.matches(regular.reg());
	}
}
